package com.shanglv.web.common.inteceptor;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GlobalPropertiesInterceptor extends HandlerInterceptorAdapter{

    private Pattern resourcePattern = Pattern.compile("\\.(css|js|doc|xls|ppt|avi|txt|gif|jpg|jpeg|bmp|png|zip|rar|swf|pdf|otf|ftl)$");

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if (!isActionPath(request) && null != modelAndView) {
            modelAndView.addAllObjects(GlobalPropertiesConfigurer.propertiesMap);
            modelAndView.addObject("contextPath",request.getContextPath());
        }
        super.postHandle(request, response, handler, modelAndView);
    }

    private boolean isActionPath(HttpServletRequest request) {
        String url = request.getServletPath();
        Matcher m = resourcePattern.matcher(url);
        return m.matches();
    }
}
